package com.lqk.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

import java.nio.charset.Charset;

public class NettyByteBuf02 {
    public static void main(String[] args) {

        // 创建ByteBuf
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello,world!", CharsetUtil.UTF_8);

        // 使用相关的方法
        if (byteBuf.hasArray()){
            byte[] content = byteBuf.array();

            // 将 content 转成字符串
            System.out.println(new String(content, Charset.forName("utf-8")));

            System.out.println("byteBuf = " + byteBuf);

            System.out.println(byteBuf.arrayOffset());
            System.out.println(byteBuf.readerIndex());
            System.out.println(byteBuf.writerIndex());
            System.out.println(byteBuf.capacity());

            int length = byteBuf.readableBytes();   // 可读取的字节数
            System.out.println(length);

            for (int i = 0; i < length; i++) {
                System.out.println((char) byteBuf.getByte(i));
            }

            System.out.println(byteBuf.getCharSequence(0,4,CharsetUtil.UTF_8));
            System.out.println(byteBuf.getCharSequence(4,6,CharsetUtil.UTF_8)); // 从索引为4的字符开始读取，读取6个字节


        }

    }
}
